import { Base } from "./base";

export class Banner extends Base {
  /**
   *
   * @param name
   * @param module
   * @param pageNum
   * @param pageSize
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async list({ cate_id, name, pageNum = 1, pageSize = 10 }) {
    const { data = {} } = await this.$http("post", "/api/banner", {
      cate_id,
      name,
      pageNum,
      pageSize
    });
    data.data &&
      data.data.map(r => {
        r.has_children = r.has_children > 0;
      });
    return data;
  }

  /**
   *
   * @param id
   * @returns {Promise<boolean>}
   */
  async del(id) {
    const { errno } = await this.$http("post", "/api/banner/del", { id });
    return errno === 0;
  }

  /**
   *
   * @param name
   * @param module
   * @param title
   * @param fid
   * @param sort
   * @returns {Promise<void>}
   */
  async add({ name, cate_id, thumb, sort, url }) {
    const { errno, data } = await this.$http("post", "/api/banner/add", {
      name,
      cate_id,
      thumb,
      sort,
      url
    });
    return errno === 0 ? data : 0;
  }

  /**
   *
   * @param id
   * @param name
   * @param fname
   * @param title
   * @param sort
   * @param has_children
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async update({ id, name, thumb, sort, cate_id, cate_name, url }) {
    const { errno } = await this.$http("post", "/api/banner/update", {
      id,
      name,
      sort,
      thumb,
      cate_id,
      cate_name,
      url
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param type
   * @returns {Promise<void>}
   */
  async detail(id, type = "detail") {
    const { data } = await this.$http("post", "/api/banner/detail", {
      id,
      type
    });
    return data;
  }
}
